package com.sunday.shoufen.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.base.BaseLazyFragment;
import com.sunday.shoufen.entity.Member;
import com.sunday.shoufen.http.ApiClient;
import com.sunday.shoufen.ui.CouponBalanceActivity;
import com.sunday.shoufen.ui.MyFansActivity;
import com.sunday.shoufen.ui.SettingActivity;
import com.sunday.shoufen.ui.mine.MyCollectionActivity;
import com.sunday.shoufen.ui.mine.MyCouponBalanceActivity;
import com.sunday.shoufen.ui.mine.MyScoreActivity;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.shoufen.base.Constants.TEST_URL;


/**
 * Created by 刘涛 on 2017/10/17.
 */

public class MineFragment extends BaseLazyFragment {

    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.logo_layout)
    RelativeLayout logoLayout;
    @Bind(R.id.btn_edit)
    ImageView btnEdit;
    @Bind(R.id.user_mobile)
    TextView userMobile;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.rl_header)
    RelativeLayout rlHeader;
    @Bind(R.id.member_count)
    TextView memberCount;
    @Bind(R.id.ll_menu1)
    LinearLayout llMenu1;
    @Bind(R.id.profit)
    TextView profit;
    @Bind(R.id.ll_menu2)
    LinearLayout llMenu2;
    @Bind(R.id.coupon_num)
    TextView couponNum;
    @Bind(R.id.ll_menu3)
    LinearLayout llMenu3;
    @Bind(R.id.ll_menu4)
    LinearLayout llMenu4;

    private Member member;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
//        initRefreshLayout();
//        isPrepared = false;
//        memberId= BaseApplication.getInstance().getMember().getId();
//        ptrFrame.autoRefresh();
        member = BaseApp.getInstance().getMember();
        getMemberInfo();
    }

    private void initView() {
        if (member == null) {
            return;
        }
        Glide.with(mContext)
                .load(member.getLogo())
                .error(R.mipmap.head_default)
                .into(userLogo);
        userMobile.setText(member.getMobile());
        userName.setText(member.getNickName());

        memberCount.setText(member.getMemberCount()+"个");
        profit.setText(member.getMonthScore().setScale(2, RoundingMode.HALF_UP)+"");
        couponNum.setText(member.getTicketMoney().setScale(2, RoundingMode.HALF_UP)+"");

    }

    private void getMemberInfo() {
        member = BaseApp.getInstance().getMember();
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().getMemberInfo(member.getId());
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    member = response.body().getResult();
                    BaseApp.getInstance().setMember(member);
                    initView();
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    @OnClick({R.id.ll_menu1, R.id.ll_menu2, R.id.ll_menu3, R.id.ll_menu4, R.id.ll_menu5})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_menu1:
                intent = new Intent(mContext, MyFansActivity.class);
                break;
            case R.id.ll_menu2:
                intent = new Intent(mContext, MyScoreActivity.class);
                break;
            case R.id.ll_menu3:
                intent = new Intent(mContext, CouponBalanceActivity.class);
                break;
            case R.id.ll_menu4:
                intent = new Intent(mContext, MyCollectionActivity.class);
                break;
            case R.id.ll_menu5:
                intent = new Intent(mContext, SettingActivity.class);
                break;
        }
        startActivity(intent);
    }


    @Override
    public void onResume() {
        super.onResume();
        getMemberInfo();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
